package com.example.electricbicycle.controller;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.example.electricbicycle.po.User;
import com.example.electricbicycle.service.UserService;
import com.example.electricbicycle.service.UserServiceImpl;

@WebServlet("/UserSearchServlet")
public class UserSearchServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;
    private final UserService userService = new UserServiceImpl();

    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        // 创建查询参数Map
        Map<String, Object> params = new HashMap<>();
        params.put("name", request.getParameter("name"));
//        params.put("role", request.getParameter("role"));

        // 解析分页参数
        int start = 0;
        int count = 10;
        try {
            start = Integer.parseInt(request.getParameter("start"));
        } catch (NumberFormatException e) {
            // 使用默认值
        }
        try {
            count = Integer.parseInt(request.getParameter("count"));
        } catch (NumberFormatException e) {
            // 使用默认值
        }

        // 执行复合查询
        List<User> userList = userService.complexQuery(params, start, count);

        // 创建角色选项列表（固定值）
        List<Map<String, String>> roleOptions = new ArrayList<>();

        Map<String, String> salesmanOption = new HashMap<>();
        salesmanOption.put("value", "0");
        salesmanOption.put("label", "业务员");
        roleOptions.add(salesmanOption);

        Map<String, String> warehouserOption = new HashMap<>();
        warehouserOption.put("value", "1");
        warehouserOption.put("label", "仓库管理员");
        roleOptions.add(warehouserOption);

        Map<String, String> salesadminOption = new HashMap<>();
        salesadminOption.put("value", "2");
        salesadminOption.put("label", "销售管理员");
        roleOptions.add(salesadminOption);

        Map<String, String> aftersalesadminOption = new HashMap<>();
        aftersalesadminOption.put("value", "3");
        aftersalesadminOption.put("label", "售后管理员");
        roleOptions.add(aftersalesadminOption);

        Map<String, String> adminOption = new HashMap<>();
        adminOption.put("value", "4");
        adminOption.put("label", "系统管理员");
        roleOptions.add(adminOption);

        // 存入请求作用域
        request.setAttribute("userList", userList);
        request.setAttribute("roleOptions", roleOptions);
        request.setAttribute("searchParams", buildSearchParams(request, params));
        request.setAttribute("currentPage", start / count + 1);
        request.setAttribute("totalPages", (int) Math.ceil((double) userService.getTotal(params) / count));

        // 转发到用户管理页面
        request.getRequestDispatcher("/admin/userlist.jsp").forward(request, response);
    }

    private String buildSearchParams(HttpServletRequest request, Map<String, Object> params) {
        StringBuilder sb = new StringBuilder();
        params.forEach((key, value) -> {
            if (value != null && !value.toString().isEmpty()) {
                sb.append("&").append(key).append("=").append(value);
            }
        });

        // 添加分页参数
        if (request.getParameter("start") != null) {
            sb.append("&start=").append(request.getParameter("start"));
        }
        if (request.getParameter("count") != null) {
            sb.append("&count=").append(request.getParameter("count"));
        }
        return sb.toString();
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        doGet(request, response);
    }
}